if exists('g:loaded_scriptV')  | finish  | en
let g:loaded_scriptV = 1

" Commands
    com! -bang -range=-1 -nargs=? -complete=expression
        \ PP
            \ if empty(<q-args>) |
            \     let s:more = &more |
            \     try |
            \         set nomore |
            \         while 1 |
            \             let s:input = input('PP> ', '', 'expression') |
            \             if empty(s:input) |
            \                 break |
            \             endif |
            \             echon "\n" |
            \             let v:errmsg = '' |
            \             try |
            \                 call scriptV#pp_command(<bang>0, -1, eval(scriptV#prepare_eval(s:input))) |
            \             catch |
            \                 echohl ErrorMsg |
            \                 echo v:exception |
            \                 echo v:throwpoint |
            \                 echohl NONE |
            \             endtry |
            \         endwhile |
            \ finally |
            \     let &more = s:more |
            \ endtry |
            \ else |
            \     let v:errmsg = '' |
            \     call scriptV#pp_command(<bang>0, <count>, eval(scriptV#prepare_eval(<q-args>))) |
            \ endif

                  "\ scriptV#pp_command(bang, lnum, value)


    let s:com_addr =  'com! -addr=other'
    exe s:com_addr '-bang -range=0      -nargs=? -complete=expression
        \ PPmsg'
            \ 'if !empty(<q-args>) |'
            \ '  let v:errmsg = "" |'
            \ '  call scriptV#ppmsg_command(<bang>0, <count>, empty(<q-args>) ? expand("<sfile>") : eval(scriptV#prepare_eval(<q-args>))) |'
            \ 'elseif &verbose >= <count> && !empty(expand("<sfile>")) |'
            \ ' echomsg expand("<sfile>").", line ".expand("<slnum>") |'
            \ 'endif'

    exe s:com_addr '-range=-1 -nargs=1 -complete=command
        \ Verbose'
                \ ':exe scriptV#
                 \verbose_command(<count> == -1
                                      \ ? ""
                                      \ : <count>,
                                \ <q-args>)'

    exe s:com_addr '-bar -count=0
        \ Scriptnames'
                \ 'call setqflist(scriptV#
                 \scriptnames_qflist()) |'
                \ 'copen |'
                \ '<count>'

    exe s:com_addr '-bar -bang -nargs=? -range=-1
        \ Messages'
                \ 'exe scriptV#messages_command(<bang>0, <count>, <q-args>)'

    exe s:com_addr '-range=-1 -nargs=? -complete=command
        \ Time'
                \ 'exe scriptV#time_command(<q-args>, <count>)'

    com! -bang -bar -range=-1 -nargs=* -complete=customlist,scriptV#complete
        \ Runtime
                \ :exe scriptV#runTime('<bang>', <f-args>)

    com! -bang -bar -nargs=* -complete=customlist,scriptV#complete
        \ Disarm
                \ :exe scriptV#disarm_command(<bang>0, <f-args>)


    " scriptV#edit_runtime_file
        "\ Edit script
        exe s:com_addr '-bar  -range=1  -nargs=1  -complete=customlist,scriptV#complete'
                \ 'EditScript'
                \ 'exe scriptV#edit_runtime_file(1, "edit", <q-args>, 0) '

        "\ 要用再开:
            "\ " 这2个一样
            "\ exe s:com_addr '-bar -bang -range=1 -nargs=1 -complete=customlist,scriptV#complete
            "\         \ Vedit'
            "\         \ 'exe scriptV#edit_runtime_file(<count>,"edit<bang>",<q-args>,  0)'
            "\         "\ \ Ve'
            "\
            "\ exe s:com_addr '-bar -bang -range=1 -nargs=1 -complete=customlist,scriptV#complete
            "\         \ Vopen'
            "\             \ 'exe scriptV#edit_runtime_file(<count>,"edit<bang>",<q-args>,  1)'
            "\
            "\ exe s:com_addr '-bar -bang -range=1 -nargs=1 -complete=customlist,scriptV#complete
            "\     \ Vsplit'
            "\             \ 'exe scriptV#edit_runtime_file( <count>, "split", <q-args>, <bang>0 )'
            "\
            "\ exe s:com_addr '-bar -bang -range=1 -nargs=1 -complete=customlist,scriptV#complete
            "\     \ Vvsplit'
            "\             \ 'exe scriptV#edit_runtime_file( <count>, "vsplit", <q-args>, <bang>0 )'
            "\
            "\ exe s:com_addr '-bar -bang -range=1 -nargs=1 -complete=customlist,scriptV#complete
            "\     \ Vtabedit'
            "\             \ 'exe scriptV#edit_runtime_file( <count>, "tabedit", <q-args>, <bang>0 )'
            "\
            "\ exe s:com_addr '-bar -bang -range=1 -nargs=1 -complete=customlist,scriptV#complete
            "\     \ Vpedit'
            "\             \ 'exe scriptV#edit_runtime_file( <count>, "pedit<bang>", <q-args>, 0 )'
            "\
            "\ exe s:com_addr '-bar -bang -range=1 -nargs=1 -complete=customlist,scriptV#complete
            "\         \ Vread'
            "\             \ 'exe scriptV#edit_runtime_file( <count>, "read", <q-args>, <bang>0 )'
        "\

"\ call eval() on the target
    nno <expr>   <Plug>scriptVFilter scriptV#filterop()
    xno <expr>   <Plug>scriptVFilter scriptV#filterop()
    ono <SID>_   _

        nmap g:  <Plug>scriptVFilter
            "\ g:{motion}
            "\ 2
        nmap g;  <Plug>scriptVFilter<SID>_
        xmap g:  <Plug>scriptVFilter



"\ au AG BufWinEnter,  * call Scripts_in_qf()
au AG BufEnter  *     call Scripts_in_qf()

    func! Scripts_in_qf()
        if exists('b:scriptS_cmd')
            if b:scriptS_cmd == 'edit'
                call Match_xX('\.vim\|\|$', '')
            else
                echo 'b:scriptS_cmd是'
                echo b:scriptS_cmd
            endif
       endif
    endf



" Filetype
    aug  scriptV
        au!
        au      FileType help call scriptV#setup_help()
        au      FileType vim  call scriptV#setup_vim() | setl iskeyword=@,48-57,_,-
        " au      FileType vim call scriptV#setup_vim()
                " 这里害得iskeyword变回default, 所以加上后面的
    aug  END

" 方便https://github.com/tpope/vim-projectionist的后续使用?
" 应该没用上 先放着
    " Projectionist
    " function! s:projectionist_detect() abort
    "     let file = get(g:, 'projectionist_file', '')
    "     let path = substitute(scriptV#locate(file)[0], '[\/]after$', '', '')
    "     if !empty(path)
    "         let reload = ":Runtime ./{open}autoload,plugin{close}/**/*.vim"
    "             " unknown function:
    "         call projectionist#append(path,
    "                     \{
    "                     \ "*": {"start": reload},
    "                     \ "*.vim": {"start": reload},
    "                     \ "plugin/*.vim":   {"command": "plugin", "alternate": "autoload/{}.vim"},
    "                     \ "autoload/*.vim": {"command": "autoload", "alternate": "plugin/{}.vim"},
    "                     \ "compiler/*.vim": {"command": "compiler"},
    "                     \ "ftdetect/*.vim": {"command": "ftdetect"},
    "                     \ "syntax/*.vim":   {"command": "syntax", "alternate": ["ftplugin/{}.vim", "indent/{}.vim"]},
    "                     \ "ftplugin/*.vim": {"command": "ftplugin", "alternate": ["indent/{}.vim", "syntax/{}.vim"]},
    "                     \ "indent/*.vim":   {"command": "indent", "alternate": ["syntax/{}.vim", "ftplugin/{}.vim"]},
    "                     \ "after/*.vim":    {"command": "after"},
    "                     \ "doc/*.txt":      {"command": "doc", "start": reload},
    "                     \}
    "             \)
    "     endif
    " endfunction
    "
    " augroup scriptV_projectionist
    "     autocmd!
    "     autocmd User ProjectionistDetect call s:projectionist_detect()
    " augroup END

